package com.petsystem.system.service.impl;

import cn.hutool.core.bean.BeanUtil;

import com.petsystem.common.utils.StringUtils;
import com.petsystem.common.core.page.TableDataInfo;
import com.petsystem.common.core.domain.PageQuery;

import org.springframework.stereotype.Service;
import com.petsystem.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.petsystem.system.domain.bo.TPetBo;
import com.petsystem.system.domain.vo.TPetVo;
import com.petsystem.system.domain.TPet;
import com.petsystem.system.mapper.TPetMapper;
import com.petsystem.system.service.ITPetService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 宠物信息Service业务层处理
 *
 * @author petsystem
 * @date 2022-02-26
 */
@Service
public class TPetServiceImpl extends ServicePlusImpl<TPetMapper, TPet, TPetVo> implements ITPetService {

    @Override
    public TPetVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<TPetVo> queryPageList(TPet pet, PageQuery pageQuery) {
        Page<TPetVo> page = baseMapper.selectPagePetList(pageQuery.build(), pet);
        return TableDataInfo.build(page);
//        LambdaQueryWrapper<TPet> lqw = buildQueryWrapper(bo);
//        Page<TPetVo> result = pageVo(pageQuery.build(), lqw);
//        return TableDataInfo.build(result);
    }

    @Override
    public List<TPetVo> queryList(TPetBo bo) {
        LambdaQueryWrapper<TPet> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<TPet> buildQueryWrapper(TPetBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TPet> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), TPet::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getGender()), TPet::getGender, bo.getGender());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), TPet::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TPet::getStatus, bo.getStatus());
        return lqw;
    }

    @Override
    public Boolean insertByBo(TPetBo bo) {
        TPet add = BeanUtil.toBean(bo, TPet.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(TPetBo bo) {
        TPet update = BeanUtil.toBean(bo, TPet.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TPet entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
